package com.zw.a.basic.a11.demo04_exercise;

import java.util.*;

//案例: 模拟斗地主发牌_有序的牌
public class Demo07_有序的牌 {
    public static void main(String[] args) {
        //1. 买牌.
        //1.1 造牌盒, 键: 牌的编号,  值: 对应的牌, 规则: 编号越小, 牌越小.
        HashMap<Integer, String> pokers = new HashMap<>();
        //1.2 造牌盒, 记录所有牌的编号.
        ArrayList<Integer> list = new ArrayList<>();
        //1.3 造牌.
        String[] colors = {"♥", "♠", "♦", "♣"};
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        //1.4 定义变量, 记录牌的索引
        int index = 0;
        //遍历点数集合
        for (String number : numbers) { //"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"
            //遍历花色集合
            for (String color : colors) {   //"♥", "♠", "♦", "♣"
                pokers.put(index, color + number);      //双列集合
                list.add(index++);                      //单列集合, 记录牌的编号
            }
        }
        //添加大小王
        pokers.put(index, "小王");      //双列集合
        list.add(index++);              //单列集合, 记录牌的编号
        pokers.put(index, "大王");      //双列集合
        list.add(index);                //单列集合, 记录牌的编号

        //2. 洗牌.  洗的是序号
        Collections.shuffle(list);

        //3. 发牌.
        TreeSet<Integer> lyf = new TreeSet<>();              //TreeSet集合的特点: 无序, 排序, 唯一.   默认升序
        TreeSet<Integer> zly = new TreeSet<>();
        TreeSet<Integer> hg = new TreeSet<>();
        TreeSet<Integer> dipai = new TreeSet<>();
        //否则你要写:  Collections.sort(List集合);
        //具体的发牌规则
        //发牌思路: 1. 最后三张作为底牌.   2. 牌的索引是0到53的, 依次和3取余,  余数为0给第一个人, 余数为1给第二个人, 余数为3给第三个人.
        for (int i = 0; i < list.size(); i++) {
            int poker =  list.get(i);      //就是每一张牌.
            if (i >= list.size() - 3) {       //51, 52, 53
                dipai.add(poker);
            } else if (i % 3 == 0) {
                lyf.add(poker);
            }else if (i % 3 == 1) {
                zly.add(poker);
            }else if (i % 3 == 2) {
                hg.add(poker);
            }
        }

        //4. 看牌.
        //看所有人和底牌的 编号.
        /*System.out.println(lyf);
        System.out.println(zly);
        System.out.println(hg);
        System.out.println(dipai);*/

        //看具体的牌
        lookPoker("刘亦菲", lyf, pokers);
        lookPoker("赵丽颖", zly, pokers);
        lookPoker("夯哥", hg, pokers);
        lookPoker("底牌", dipai, pokers);
    }

    //4. 定义方法, 用来看牌

    /**
     * 根据玩家手中的编号找对应的牌.
     * @param name      玩家的名字
     * @param ts        玩家手中的牌的编号
     * @param pokers    双列集合, 记录的是 牌的编号和牌的关系, 规则: 编号越小, 牌越小.
     */
    public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> pokers) {
        System.out.print(name + "的牌是: ");
        for (int pokerNumber : ts) {
            System.out.print(pokers.get(pokerNumber) + " ");
        }
        System.out.println();
    }
}
